package com.sweets.look.ui.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sweets.look.R;


public class MarkEditDialog {
    public interface OnCustomDialogListener {
        void back(String arg1, String arg2, String arg3);
    }

    private final Display display;
    private boolean isLimited;
    private final Context mContext;
    private Dialog mDialog;
    private TextView mDialogMsg;
    private EditText mDialogMsgEdit;
    private AutoCompleteTextView mDialogOtherEdit;
    private TextView mDialogTitle;
    private EditText mDialogTitleEdit;
    private TextView mNegativeBtn;
    private ImageView mOperateBtn;
    private TextView mPositiveBtn;
    private View mView;

    public MarkEditDialog(Context context) {
        super();
        this.mContext = context;
        this.display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    }

    public MarkEditDialog builder() {
        this.mView = LayoutInflater.from(this.mContext).inflate(R.layout.widget_edit_dialog,
                (ViewGroup) ((Activity) this.mContext).findViewById(android.R.id.content), false);//16908290
        RelativeLayout localRelativeLayout = (RelativeLayout) this.mView.findViewById(R.id.dialog_layout);
        this.mDialogTitle = ((TextView) this.mView.findViewById(R.id.dialog_title));
        this.mDialogTitle.setVisibility(View.GONE);
        this.mDialogTitleEdit = ((EditText) this.mView.findViewById(R.id.dialog_title_edit));
        this.mDialogTitleEdit.setVisibility(View.GONE);
        this.mDialogMsgEdit = ((EditText) this.mView.findViewById(R.id.dialog_msg_edit));
        this.mDialogMsgEdit.setVisibility(View.GONE);
        this.mDialogOtherEdit = ((AutoCompleteTextView) this.mView.findViewById(R.id.dialog_other_edit));
        this.mDialogOtherEdit.setVisibility(View.GONE);
        this.mPositiveBtn = ((TextView) this.mView.findViewById(R.id.dialog_btn_positive));
        this.mPositiveBtn.setVisibility(View.GONE);
        this.mOperateBtn = ((ImageView) this.mView.findViewById(R.id.dialog_copy_msg));
        this.mOperateBtn.setVisibility(View.GONE);
        this.mNegativeBtn = ((TextView) this.mView.findViewById(R.id.dialog_btn_negative));
        this.mNegativeBtn.setVisibility(View.GONE);
        this.mDialogMsg = ((TextView) this.mView.findViewById(R.id.dialog_msg));
        this.mDialogMsg.setVisibility(View.GONE);
        this.mDialog = new Dialog(this.mContext, R.style.DialogTheme);
        this.mDialog.setContentView(this.mView);
        int width = this.display.getWidth();
        int height = this.display.getHeight();
        int realWidth = width >= height ? height / 6 * 5 : width / 6 * 5;

        localRelativeLayout.setLayoutParams(new FrameLayout.LayoutParams(realWidth, -2));
        this.mDialog.getWindow().setWindowAnimations(R.style.DialogAnimation);
        return this;
    }

    private void deInputLimit(EditText[] ets) {
        int v2 = ets.length;
        int v1 = 0;
        while (v1 < v2) {
            ets[v1].setInputType(1);
            ++v1;
        }
    }

    public void dismiss() {
        if (this.mDialog != null && (this.mDialog.isShowing())) {
            this.mDialog.dismiss();
        }
    }

    public MarkEditDialog setCancelable(boolean cancel) {
        this.mDialog.setCancelable(cancel);
        return this;
    }

    public MarkEditDialog setCanceledOnTouchOutside(boolean cancel) {
        this.mDialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    public MarkEditDialog setInputLimit(boolean b) {
        this.isLimited = b;
        return this;
    }

    public MarkEditDialog setMessage(String msg) {
        this.mDialogMsg.setVisibility(View.VISIBLE);
        this.mDialogMsg.setText(msg);
        return this;
    }

    public MarkEditDialog setMsgHint(String msgHint, String msg) {
        this.mDialogMsgEdit.setVisibility(View.VISIBLE);
        this.mDialogMsgEdit.setHint(msgHint);
        this.mDialogMsgEdit.setText(msg);
        this.mView.findViewById(R.id.dialog_msg_driver).setVisibility(View.VISIBLE);
        return this;
    }

    public MarkEditDialog setNegativeButton(String text, final View.OnClickListener listener) {
        this.mNegativeBtn.setVisibility(View.VISIBLE);
        this.mNegativeBtn.setText(text);
        this.mNegativeBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(v);
                }

                ((InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                        mNegativeBtn.getWindowToken(), 0);
                mDialog.dismiss();
            }
        });
        return this;
    }

    public MarkEditDialog setOperateButton(int icon, final View.OnClickListener listener) {
        this.mOperateBtn.setVisibility(View.VISIBLE);
        this.mOperateBtn.setImageResource(icon);
        this.mOperateBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(v);
                }
            }
        });
        return this;
    }

    public MarkEditDialog setOtherHint(String otherHint, String other, String[] keywords) {
        ArrayAdapter v0 = new ArrayAdapter(this.mContext, R.layout.item_folder_input, ((Object[]) keywords));
        this.mDialogOtherEdit.setVisibility(View.VISIBLE);
        this.mDialogOtherEdit.setAdapter(v0);
        this.mDialogOtherEdit.setHint(otherHint);
        this.mDialogOtherEdit.setText(other);
        this.mView.findViewById(R.id.dialog_other_driver).setVisibility(View.VISIBLE);
        return this;
    }

    public MarkEditDialog setPositiveButton(String text, final OnCustomDialogListener listener) {
        this.mPositiveBtn.setVisibility(View.VISIBLE);
        this.mPositiveBtn.setText(text);
        this.mPositiveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String v3 = mDialogTitleEdit.getText().toString();
                String v1 = mDialogMsgEdit.getText().toString();
                String v2 = mDialogOtherEdit.getText().toString();
                if (mDialogTitleEdit.getVisibility() == View.GONE) {
                    v3 = "default";
                }

                if (mDialogMsgEdit.getVisibility() == View.GONE) {
                    v1 = "default";
                }

                if (mDialogOtherEdit.getVisibility() == View.GONE) {
                    v2 = "";
                }

                if (listener != null && v3.length() > 0 && v1.length() > 0) {
                    listener.back(v3, v1, v2);
                }

                ((InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                        mPositiveBtn.getWindowToken(), 0);
                mDialog.dismiss();
            }
        });
        return this;
    }

    public MarkEditDialog setTitle(String title) {
        this.mDialogTitle.setVisibility(View.VISIBLE);
        this.mDialogTitle.setText(title);
        return this;
    }

    public MarkEditDialog setTitleHint(String titleHint, String title) {
        this.mDialogTitleEdit.setVisibility(View.VISIBLE);
        this.mDialogTitleEdit.setHint(titleHint);
        this.mDialogTitleEdit.setText(title);
        this.mView.findViewById(R.id.dialog_title_driver).setVisibility(View.VISIBLE);
        return this;
    }

    public void show() {
        if (!this.isLimited) {
            this.deInputLimit(new EditText[]{this.mDialogTitleEdit, this.mDialogMsgEdit, this.mDialogOtherEdit});
        }

        this.mDialog.show();
    }
}

